#!/bin/sh

# only for use as a "zcip" callback script
if [ "x$interface" = x ]; then
	exit 0
fi

LOG="logger -t zcip"

# include global
. /etc/scripts/global.sh

# in pure pppoe mode no need routing to from lan 2 wan
if [ "$vpnEnabled" = "on" ] && [ "$vpnType" = "0" ] && [ "$vpnPurePPPOE" = "1" ]; then
    netmask=32
else
    netmask=16
fi

# zcip should start on boot/resume and various media changes
case "$1" in
init)
	# for now, zcip requires the link to be already up,
	# and it drops links when they go down.  that isn't
	# the most robust model...
	exit 0
	;;
config)
	if [ "x$ip" = x ]; then
		exit 0
	fi
	# remember $ip for $interface, to use on restart
	if [ "x$ip" != x -a -w "$ip.$interface" ]; then
		echo $ip > "$ip.$interface"
	fi
	$LOG "Add ip adress $ip/$mask to interface $interface and interface up."
	ip address add dev $interface scope link local "$ip/$netmask" broadcast +
	ip link set $interface up
	# add routes configured in web
	if [ -f /etc/routes_replace ]; then
	    $LOG "Add user routes for $lan_if $wan_if interfaces."
	    /etc/routes_replace replace $lan_if $interface
	fi
	# Add route to multicast subnet
	if [ "$igmpEnabled" = "1" -o "$UDPXYMode" != "0" ]; then
	    $LOG "Add route to multicast subnet."
	    ip route replace "$mcast_net" dev "$interface"
	fi
	if [ -f /bin/cdp-send ] && [ -f /etc/scripts/config-cdp.sh ]; then
	    $LOG "Send Cisco Discovery request from $interface interface."
	    config-cdp.sh &
	fi
	exit 0
	;;
deconfig)
	if [ x$ip = x ]; then
	    exit 0
	fi
	exec ip address del dev $interface local $ip > /dev/null 2>&1
	exit 0
	;;
esac
exit 0
